package com.neusoft.neuiotms.dcom.converter.incident;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcom.dto.incident.IncidentHandleDTO;
import com.neusoft.neuiotms.dcom.model.incident.IncidentHandle;
import com.neusoft.neuiotms.dcom.model.incident.IncidentHandleAttachment;
import com.neusoft.neuiotms.dcom.repository.incident.IncidentHandleRepository;

/**
 * @author WYS
 * @date 2020年8月18日 下午2:02:51
 */
@Component
public class IncidentHandleConverter extends AbstractConverter<IncidentHandle, IncidentHandleDTO> {

    @Autowired
    private IncidentHandleRepository incidentHandleRepository;
    @Autowired
    private IncidentHandleAttachmentConverter incidentHandleAttachmentConverter;

    @Override
    public IncidentHandle toModel(final IncidentHandleDTO dto) {
        IncidentHandle model;
        if (dto.isNew()) {
            model = new IncidentHandle();
            model.setCreateTime(new Date());
        } else {
            model = this.incidentHandleRepository.getOne(dto.getId());
            model.setUpdateTime(new Date());
        }
        model.setTaskId(dto.getTaskId());
        model.setWorkNum(dto.getWorkOrderNum());
        if ((dto.getAttachments() != null) && !dto.getAttachments().isEmpty()) {
            final List<IncidentHandleAttachment> list =
                    this.incidentHandleAttachmentConverter.toListModel(dto.getAttachments());
            list.forEach(t -> {
                t.setIncidentHandle(model);
            });
            model.setAttachments(list);
        }
        model.setEventCause(dto.getEventCause());
        model.setPlan(dto.getPlan());
        model.setResult(dto.getResult());
        model.setUnfinishedCause(dto.getUnfinishedCause());
        model.setExecuteUserCode(dto.getExecuteUserCode());
        model.setExecuteUserName(dto.getExecuteUserName());
        model.setStatus(dto.getStatus());

        return model;
    }

    @Override
    public IncidentHandleDTO toDTO(final IncidentHandle model, final boolean forListView) {
        final IncidentHandleDTO dto = new IncidentHandleDTO();
        dto.setId(model.getId());
        dto.setTaskId(model.getTaskId());
        dto.setWorkOrderNum(model.getWorkNum());
        if (!forListView) {
            dto.setAttachments(
                    this.incidentHandleAttachmentConverter.toListDTO(model.getAttachments()));
        }

        dto.setEventCause(model.getEventCause());
        dto.setPlan(model.getPlan());
        dto.setResult(model.getResult());
        dto.setUnfinishedCause(model.getUnfinishedCause());
        dto.setExecuteUserCode(model.getExecuteUserCode());
        dto.setExecuteUserName(model.getExecuteUserName());
        dto.setCreateTime(model.getCreateTime());
        dto.setUpdateTime(model.getUpdateTime());
        dto.setStatus(model.getStatus());

        return dto;
    }

}
